﻿using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class InOutFlowFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public InOutFlowFrm()
        {
            InitializeComponent();
            moneycbb.DataSource = db.CashAccounts.ToList();
            moneycbb.DisplayMember = "TradeTypeName";
            moneycbb.ValueMember = "Id";
            moneycbb.SelectedValue = 0;
            //var start = startDatePiker.Value;
            //var end = endDatePicker.Value;

            //var list = from cl in db.CashAccountLogs.Where(o => o.MakerTime > start && o.MakerTime < end).ToList()
            //           join u in db.Users.ToList() on cl.MakerId equals u.Id
            //           join c in db.CashAccounts.ToList() on cl.CashAccountId equals c.Id
            //           select new
            //           {
            //               CashAccountName = c.TradeTypeName,
            //               cl.OriginalAmount,
            //               cl.VariableAmount,
            //               Amount = cl.CurrentAmount,
            //               MakerName = u.Name,
            //               cl.CreateTime,
            //               cl.Source,
            //               cl.RelatedObject
            //           };
            //dataGridView1.DataSource = list.ToList();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            Helper.ExportDgv(dataGridView1, "出入金流水");
            //MessageBox.Show("导出功能受限！");
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            var list = getSql();
            dataGridView1.DataSource = list;
            inSumTxt.Text = list.Where(o => o.VariableAmount > 0).Sum(o => o.VariableAmount).ToString();
            outSumTxt.Text = (-list.Where(o => o.VariableAmount < 0).Sum(o => o.VariableAmount)).ToString();
        }
        private List<InOutFlowModel> getSql()
        {
            var startTime = startDatePiker.Value.Date;
            var endTime = endDatePicker.Value.AddDays(1).Date;
            var accountId = 0;
            if (moneycbb.SelectedValue != null) accountId = (int)moneycbb.SelectedValue;

            var list = (from cl in db.CashAccountLogs.Where(o => o.MakerTime > startTime && o.MakerTime < endTime && (accountId <= 0 || o.CashAccountId == accountId)).ToList()
                        join u in db.Users.ToList() on cl.MakerId equals u.Id into clu
                        from clus in clu.DefaultIfEmpty()
                        join c in db.CashAccounts.ToList() on cl.CashAccountId equals c.Id
                        select new InOutFlowModel
                        {
                            CashAccountName = c.TradeTypeName,
                            OriginalAmount = cl.OriginalAmount,
                            VariableAmount = cl.VariableAmount,
                            Amount = cl.CurrentAmount,
                            MakerName =(clus==null||string.IsNullOrEmpty(clus.Name))?"": clus.Name,
                            CreateTime = cl.CreateTime,
                            Source = cl.Source,
                            RelatedObject = cl.RelatedObject
                        }).ToList();
            return list;
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {

                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column.Name == "Source")
                {
                    var orderNo = dataGridView1.CurrentRow.Cells["Source"].Value;
                    if (orderNo != null && (orderNo.ToString().StartsWith("J") || orderNo.ToString().StartsWith("D")))
                    {
                        var show = new SaleOrderShow(orderNo.ToString());
                        show.Show();
                    }
                }
                else if (column.Name == "RelatedObject")
                {
                    var orderNo = dataGridView1.CurrentRow.Cells["RelatedObject"].Value;
                    if (orderNo != null && (orderNo.ToString().StartsWith("J") || orderNo.ToString().StartsWith("D")))
                    {
                        var show = new SaleOrderShow(orderNo.ToString());
                        show.Show();
                    }
                }

            }
        }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var sql = getSql();
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column != null)
            {
                var list = new List<SaleTableModel>();
                if (column.Name == "CashAccountName")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.CashAccountName).ToList();
                }
                else if (column.Name == "VariableAmount")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.VariableAmount).ToList();
                }
                else if (column.Name == "CreateTime")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.CreateTime).ToList();
                }          
            }
        }

        private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var sql = getSql();
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column != null)
            {
                var list = new List<SaleTableModel>();
                if (column.Name == "CashAccountName")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.CashAccountName).ToList();
                }
                else if (column.Name == "VariableAmount")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.VariableAmount).ToList();
                }
                else if (column.Name == "CreateTime")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.CreateTime).ToList();
                }
            }
        }
    }
    public class InOutFlowModel
    {
        public string CashAccountName { get; set; }
        public decimal OriginalAmount { get; set; }
        public decimal VariableAmount { get; set; }
        public decimal Amount { get; set; }
        public string MakerName { get; set; }
        public DateTime? CreateTime { get; set; }
        public string Source { get; set; }
        public string RelatedObject { get; set; }
    }
}
